package com.bulletelc.apollopre.module.main.center.address.list;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.AddressDetailBean;
import com.bulletelc.apollopre.framework.MVPBaseActivity;
import com.bulletelc.apollopre.module.main.center.address.edit.EditAddressActivity;
import com.bulletelc.apollopre.utils.ToastUtil;
import com.bulletelc.apollopre.widget.FoolBar;

import java.util.ArrayList;
import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/23
 * desc  : 地址列表界面
 */

public class AddressActivity extends MVPBaseActivity<AddressContract.View, AddressPresenter> implements AddressContract.View,
        View.OnClickListener, AddressListAdapter.OnCheckboxClickListener, AddressListAdapter.OnCardViewClickListener,
        FoolBar.ClickCallback {

    // views
    private FoolBar flBar;
    private TextView tvNoAddress;
    private RecyclerView rcvAddressList;
    private TextView tvAddAddress;

    // data
    public static final int EDIT = 2; // Intent打开Activity
    List<AddressDetailBean> addressList;
    private AddressListAdapter adapter;
    private boolean isChoose = false; // 是否选择地址状态，默认false

    @Override
    public int getLayoutRes() {
        return R.layout.address_list_activity;
    }

    public void assignViews() {
        flBar = findView(R.id.flBar);
        flBar.setTitle(this.getResources().getString(R.string.center_address));
        tvNoAddress = findView(R.id.tvNoAddress);
        rcvAddressList = findViewById(R.id.rcvAddressList);
        tvAddAddress = findView(R.id.tvAddAddress);
    }

    @Override
    public void setListener() {
        flBar.setClickCallback(this);
        tvAddAddress.setOnClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            isChoose = intent.getBooleanExtra(IntentCode.EXTRA_KEY_FROM_CLEARING, false);
        }
        addressList = new ArrayList<>();
        adapter = new AddressListAdapter(this, addressList, isChoose, presenter);
        rcvAddressList.setAdapter(adapter);
        rcvAddressList.setLayoutManager(new LinearLayoutManager(this.getApplication()));
        adapter.setOnCheckboxClickListener(this);
        adapter.setOnCardViewClickListener(this);

        presenter.getAddressList();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case IntentCode.REQUEST_CODE_ADDRESS_LIST_TO_ADD: //添加
                if (resultCode == IntentCode.RESULT_CODE_ADD_ADDRESS_TO_LIST) {
                    presenter.getAddressList();
                }
                break;
            case EDIT:  //编辑

                break;
        }
    }

    @Override
    protected AddressPresenter createPresenter() {
        return new AddressPresenter();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvAddAddress:
                Intent intent = new Intent(getApplicationContext(), EditAddressActivity.class);
                startActivityForResult(intent, IntentCode.REQUEST_CODE_ADDRESS_LIST_TO_ADD);
                break;
        }
    }

    @Override
    public void onSuccessAddressList(List<AddressDetailBean> addresses) {
        if (addresses != null && addresses.size() > 0) {
            tvNoAddress.setVisibility(View.GONE);
            addressList.clear();
            addressList.addAll(addresses);
            adapter.notifyDataSetChanged();
//            tvNoAddress.setVisibility(View.GONE);
            rcvAddressList.setVisibility(View.VISIBLE);
        } else {
            tvNoAddress.setVisibility(View.VISIBLE);
            rcvAddressList.setVisibility(View.GONE);
        }

    }

    @Override
    public void onFailAddressList(String fail) {
        Toast.makeText(this, fail, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onErrorAddressList(String error) {
        ToastUtil.showLongToast(this, R.string.network_msg);
    }

    @Override
    public void onSuccessDelete(int position) {
        addressList.remove(position);
        if (addressList.size() > 0) {
            adapter.notifyDataSetChanged();
        } else {
            tvNoAddress.setVisibility(View.VISIBLE);
            rcvAddressList.setVisibility(View.GONE);
        }
    }

    @Override
    public void onFailDelete(String fail) {
        Toast.makeText(this, fail, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onErrorDelete(String error) {
        ToastUtil.showLongToast(this, R.string.network_msg);
    }

    @Override
    public void onSuccessDefault() {
        presenter.getAddressList();
    }

    @Override
    public void onFailDefault(String fail) {
        Toast.makeText(this, fail, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onErrorDefault(String error) {
        ToastUtil.showLongToast(this, R.string.network_msg);
    }

    @Override
    public void onCheckboxClick(int position) {
        int addressId = addressList.get(position).getId();
        presenter.setDefaultAddress(addressId);
    }

    @Override
    public void onCardViewClick(View view, int position) {
        AddressDetailBean address = addressList.get(position);
        Intent intent = new Intent();
        intent.putExtra(IntentCode.EXTRA_KEY_ADDRESS_DATA, address);
        setResult(IntentCode.RESULT_CODE_ADDRESS_LIST_RETURN_CLEARING, intent);
        finish();
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }
}
